package com.alm.token.ui.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.GlideApp;
import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.ui.data.bean.WalletLogDetaiData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.BaseView;
import com.alm.token.utils.ActionBarHelper;
import com.alm.token.utils.CommonUtil;
import com.alm.token.utils.EmptyUtils;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LogDetailActivity extends BaseActivity implements BaseView<WalletLogDetaiData> {

    public static final int DRAW_TYPE = 1, BILL_TYPE = 2;
    @BindView(R.id.img_bizhongicon)
    ImageView imgBizhongicon;
    @BindView(R.id.tv_bizhongtv)
    TextView tvBizhongtv;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_transfertype)
    TextView tvTransfertype;
    @BindView(R.id.ly_drawbody)
    LinearLayout lyDrawbody;
    @BindView(R.id.tv_transferdate)
    TextView tvTransferdate;
    @BindView(R.id.tv_remark)
    TextView tvRemark;
    @BindView(R.id.img_CoinApiBean_icon)
    ImageView imgCoinApiBeanIcon;
    @BindView(R.id.tv_CoinApiBean_name)
    TextView tvCoinApiBeanName;
    @BindView(R.id.ly_CoinApiBean_body)
    LinearLayout lyCoinApiBeanBody;

    private int type = 1, id = 0;

    @Override
    protected MyPreenter initPreenter() {
        return preenter = new MyPreenter(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_logdetail;
    }

    @Override
    protected void initVariables() {
        type = getIntent().getIntExtra("LogDetailActivity_type", DRAW_TYPE);
        id = getIntent().getIntExtra("LogDetailActivity_id", 0);
    }

    @Override
    protected void initViews() {
        new ActionBarHelper(this).setTitle(getString(R.string.log_hint_itemdetail));
    }

    @Override
    protected void loadData() {
//        if (eggGoodsBeans == null)
        if (type == DRAW_TYPE) {
            preenter.getDrawLogDetailData(String.valueOf(id));
        } else {
            preenter.getWalletLogDetailData(String.valueOf(id));
        }
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void getData(WalletLogDetaiData walletLogDetaiData) {
        WalletLogDetaiData.DataBean dataBean;
        WalletLogDetaiData.DataBean.CoinApiBean coinApiBean = null;
        List<WalletLogDetaiData.DataBean.AdditionalListBean> additionalList = new ArrayList<>();
        if (walletLogDetaiData.getStatus() == 0) {
            dataBean = walletLogDetaiData.getData();
            if (EmptyUtils.isNotEmpty(dataBean)) {
                coinApiBean = dataBean.getCoinApi();
                additionalList = dataBean.getAdditionalList();
                /**
                 * channelTypeStr : 提现
                 * moneyTypeStr : ETH
                 * money : -1
                 * moneyIcon : https://w.iotoken.net/static/images/35656046013.png
                 * createTime : 1563353379000
                 * msg : 提现---werwrwrsdfasdfdfaf
                 * additionalList : [{"name":"地址","value":"werwrwrsdfasdfdfaf"}]
                 */
                GlideApp.with(LogDetailActivity.this).load(dataBean.getMoneyIcon())
                        .listener(new RequestListener<Drawable>() {
                            @Override
                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
//                                if (EmptyUtils.isNotEmpty(resource)) {
//                                    CommonUtil.changeSVGColor(LogDetailActivity.this, imgBizhongicon, resource, R.color.black);
//                                    return true;
//                                }
                                return false;
                            }
                        })
                        .error(R.drawable.icon_arctic).into(imgBizhongicon);
                tvBizhongtv.setText(dataBean.getMoneyTypeStr());


                if (dataBean.getMoney() < 0) {
                    tvMoney.setText(CommonUtil.saveToPoint4WithNo(dataBean.getMoney()));
                    tvMoney.setTextColor(getResources().getColor(R.color.lehuobao_zhichu));
                } else {
                    tvMoney.setText("+" + CommonUtil.saveToPoint4WithNo(dataBean.getMoney()));
                    tvMoney.setTextColor(getResources().getColor(R.color.colorgreen1));
                }

                tvTransfertype.setText(dataBean.getChannelTypeStr());
                tvRemark.setText(dataBean.getMsg());
                tvTransferdate.setText(CommonUtil.getDateTimeFromMillisecond2(dataBean.getCreateTime()));
            }
            if (EmptyUtils.isNotEmpty(additionalList)) {
                /**
                 * name : 地址  hash      Address  Transaction hash   목적지의 주소        거래 해시     目标地址  交易hash
                 * value : werwrwrsdfasdfdfaf
                 */
                lyDrawbody.setVisibility(View.VISIBLE);
                for (WalletLogDetaiData.DataBean.AdditionalListBean bean : additionalList) {
                    View layout = LayoutInflater.from(this).inflate(R.layout.layout_logdetail_item, null);
                    TextView tvName = layout.findViewById(R.id.tv_name);
                    TextView tvValue = layout.findViewById(R.id.tv_value);
                    TextView tv_copyadress = layout.findViewById(R.id.tv_copyadress);
                    tvName.setText(bean.getName());
                    tvValue.setText(bean.getValue());


                    if ("地址  hash  Address  Transaction hash  목적지의 주소   거래 해시   目标地址  交易hash".contains(bean.getName())) {
                        tv_copyadress.setVisibility(View.VISIBLE);
                        tv_copyadress.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                                // 将文本内容放到系统剪贴板里。
                                if (cm != null) {
                                    // 创建普通字符型ClipData
                                    ClipData mClipData = ClipData.newPlainText("Label", tvValue.getText().toString());
                                    // 将ClipData内容放到系统剪贴板里。
                                    cm.setPrimaryClip(mClipData);
                                }
                                showToast(getString(R.string.profile_hint_item14));
                            }
                        });
                    }


                    lyDrawbody.addView(layout);
                }
            } else {
                lyDrawbody.setVisibility(View.GONE);
            }
            if (EmptyUtils.isNotEmpty(coinApiBean)) {

                tvCoinApiBeanName.setText(coinApiBean.getName());
                GlideApp.with(LogDetailActivity.this).load(coinApiBean.getIcon()).into(imgCoinApiBeanIcon);
                WalletLogDetaiData.DataBean.CoinApiBean finalCoinApiBean = coinApiBean;
                tvCoinApiBeanName.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent2 = new Intent(LogDetailActivity.this, JackpotAddressActivity.class);
                        intent2.putExtra("moneyAdress", finalCoinApiBean.getUrl());
                        intent2.putExtra("type", 11);
                        startActivity(intent2);
                    }
                });
                lyCoinApiBeanBody.setVisibility(View.VISIBLE);
            }
        } else {
            showToast(walletLogDetaiData.getMsg());
        }
    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(getActivity(),PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType",PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            getActivity().finish();
            showToSetPaypswDialog();
        } else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void onError(String error) {

        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {

        showWaitDialog();
    }

    @Override
    public void hideProgress() {

        hideWaitDialog();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
